package com.dty.system.service.impl;

import com.dty.model.base.ClickComment;
import com.dty.system.mapper.ClickCommentMapper;
import com.dty.system.service.ClickCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClickCommentServiceImpl implements ClickCommentService {

    @Resource
    private ClickCommentMapper clickCommentMapper;

    @Override
    public void addClickComment(ClickComment clickComment) {
        ClickComment click = new ClickComment();
        //判断是不是一级评论
        if (clickComment.getParentId() == 0) {
            clickCommentMapper.addClickComment(clickComment);
        } else if (clickComment.getParentId() != 0) {
            ClickComment comment = clickCommentMapper.selectParentById(clickComment.getParentId());
            final int i = comment.getId().intValue();
            click.setParentId(Integer.valueOf(i));
            click.setCommentText(clickComment.getCommentText());
            click.setColumnId(clickComment.getColumnId());
            click.setUserName(clickComment.getUserName());
            click.setUserImage(clickComment.getUserImage());
            click.setCreateTime(new Date());
            click.setStatus(clickComment.getStatus());
            clickCommentMapper.addClickComment(click);

        }
    }

    @Override
    public List<ClickComment> selectClickComment(Integer columnId) {
        List<ClickComment> list = clickCommentMapper.selectClickComment(columnId);
        // 获取所有的一级评论
        List<ClickComment> collect = list.stream().filter(ClickComment1 -> ClickComment1.getParentId() == 0).collect(Collectors.toList());
        // 从一级评论中获取回复评论
        for (ClickComment clickComment : collect) {
            getChildren(list, clickComment);
        }
        return collect;
    }

    /**
     * 递归查询所有评论
     *
     * @param list   所有一级评论
     * @param parent 接收的参数
     */
    public void getChildren(List<ClickComment> list, ClickComment parent) {
        for (ClickComment clickComment : list) {
            if ((long) clickComment.getParentId() == parent.getId()) {
                if (null == parent.getChildren()) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(clickComment);
                getChildren(list, clickComment);
            }
        }
    }

    @Override
    public void deleteClickComment(Integer id) {
        if (id == null) {
            throw new RuntimeException("id传入错误");
        }
            List<ClickComment> list = clickCommentMapper.selectClick();
            List<Long> objects = new ArrayList<>();
            final List<Long> children = getChildren(list, (long) id, objects);
            children.add((long)id);
            clickCommentMapper.deleteClickCommentByIds(children);


    }

    public List<Long> getChildren(List<ClickComment> list, Long id, List<Long> deleteList) {
        for (ClickComment clickComment : list) {
            if (clickComment.getParentId() == id.intValue()) {
                deleteList.add(clickComment.getId());
                getChildren(list, clickComment.getId(), deleteList);
            }
        }
        return deleteList;
    }


}
